﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.Windows;
using System.Windows.Media;

namespace Xceed.Silverlight.DataGrid
{
  public class DataGridBehaviors : DependencyObject
  {
    #region ClipElement Attached Dependency Property

    public static readonly DependencyProperty ClipElementProperty = DependencyProperty.RegisterAttached(
      "ClipElement",
      typeof( bool ),
      typeof( DataGridBehaviors ),
      new PropertyMetadata( false, new PropertyChangedCallback( DataGridBehaviors.OnClipElementChanged ) )
    );

    public static void SetClipElement( FrameworkElement element, bool value )
    {
      element.SetValue( DataGridBehaviors.ClipElementProperty, value );
    }

    public static bool GetClipElement( FrameworkElement element )
    {
      return ( bool )element.GetValue( DataGridBehaviors.ClipElementProperty );
    }

    private static void OnClipElementChanged( DependencyObject source, DependencyPropertyChangedEventArgs args )
    {
      FrameworkElement element = source as FrameworkElement;
      if(element == null)
        throw new DataGridInternalException();

      bool oldValue = ( bool )args.OldValue;
      bool newValue = ( bool )args.NewValue;
      if( oldValue != newValue )
      {
        if( newValue )
        {
          element.Loaded += new RoutedEventHandler( DataGridBehaviors.ScrollablePanelLoaded );
          element.SizeChanged += new SizeChangedEventHandler( DataGridBehaviors.ScrollablePanelSizeChanged );
        }
        else
        {
          element.Loaded -= new RoutedEventHandler( DataGridBehaviors.ScrollablePanelLoaded );
          element.SizeChanged -= new SizeChangedEventHandler( DataGridBehaviors.ScrollablePanelSizeChanged );
        }
      }
    }

    private static void ScrollablePanelSizeChanged( object sender, SizeChangedEventArgs e )
    {
      DataGridBehaviors.DoClip((FrameworkElement)sender);
    }

    private static void ScrollablePanelLoaded( object sender, RoutedEventArgs e )
    {
      DataGridBehaviors.DoClip((FrameworkElement)sender);
    }

    private static void DoClip(FrameworkElement element)
    {
      element.Clip = new RectangleGeometry()
      {
        Rect = new Rect( 0, 0, element.ActualWidth, element.ActualHeight )
      };
    }

    #endregion ClipElement Attached Dependency Property
  }
}
